#include <iostream>

struct pcb{
    char name[10];
    float arrivetime;
    float servicetime;
    float starttime;
    float finishtime;
    float zztime;
    float dqzztime;
};
pcb a[100];

void input(pcb *p,int N){
    int i;
    printf("\n请输入各进程信息；进程名 到达时间 所需运行时间： （例如："
           "a 0 100）\n\n");
    for(i=0;i<=N-1;i++){
        printf("请输入进程%d的信息:\t",i+1);
        scanf("\t\t\t%s%f%f",&p[i].name,&p[i].arrivetime,&p[i].servicetime);
    }
}

void print(pcb *p,float arrivetime,float servicetime,float starttime,
           float finishtime,float zztime,float dqzztime,int N) {
    int k;
    printf("\n\n采用先来先服务算法进程调度的顺序是：");
    printf("%s", p[0].name);
    for (k = 1; k < N; k++)
        printf("-->%s", p[k].name);
    printf("\n\n具体进程调度信息:\n");
    printf("\t进程名 到达时间 运行时间 开始时间 结束时间 周转时间 带权周转时间\n");
    for (k = 0; k <= N - 1; k++) {
        printf("\t  %s\t%-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\t %-.2f\n",
               p[k].name, p[k].arrivetime, p[k].servicetime,
               p[k].starttime, p[k].finishtime, p[k].zztime, p[k].dqzztime);
        getchar();
    }
}
void sort(pcb *p, int N) {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= i; j++)
            if (p[i].arrivetime < p[j].arrivetime) {
                pcb temp;
                temp = p[i];
                p[i] = p[j];
                p[j] = temp;
            }
    }
}

void deal(pcb *p,float arrivetime,float servicetime,float starttime,float finishtime,float &zztime,float &dqzztime,int N)
{
    int k;
    for(k=0;k<N;k++)
    {
        if(k==0){
            p[k].starttime=p[k].arrivetime;
            p[k].finishtime=p[k].arrivetime+p[k].servicetime;
        }
        else{
            if(p[k].arrivetime<=p[k-1].finishtime)
                p[k].starttime=p[k-1].finishtime;
            else
                p[k].starttime=p[k].arrivetime;
            p[k].finishtime=p[k].starttime+p[k].servicetime;
        }
    }
    for(k=0;k<N;k++){
        p[k].zztime=p[k].finishtime-p[k].arrivetime;
        p[k].dqzztime=p[k].zztime/p[k].servicetime;
    }
}

void FCFS(pcb *p,int N)
{
    float arrivetime=0,servicetime=0,starttime=0,finishtime=0,zztime=0,
            dqzztime=0;
    sort(p,N);
    deal(p,arrivetime,servicetime,starttime,finishtime,zztime,
         dqzztime,N);
    print(p,arrivetime,servicetime,starttime,finishtime,zztime,
          dqzztime,N);
    getchar();
}

int main(){
    int N;
    printf("\t\t------------------先来先服务算法------------------"
           "-------\n\n");
    printf("输入进程数目:");
    scanf("%d",&N);
    input(a,N);
    FCFS(a,N);
    return 0;
}









